<?php
class StaffsController extends AppController {

	var $name = 'Staffs';
	
	var $components = array('Email');
	
	function beforeFilter(){
		if($this->Session->read('Auth.User')){
			$this->Auth->allow('*');
		}
	}
	
	function contact(){
		$post = '';
		$this->Email->from 		= '<jobs@wakhoknepal.com>';
		$this->Email->bcc 		= array('test1@ramhariregmi.com.np');
		$this->Email->sendAs 	= 'html';
		$this->Email->delivery 	= 'mail';
		$this->Email->template 	= 'default';
		$this->Email->to 		= 'mail@ramhariregmi.com.np';
		$this->Email->subject	= 'Enquiry on Job Portal Site.';
		if(isset($_POST['Temporary-staffing'])){
			$post .= '<p><label><b>Inquiry: </b></label>Temporary Staffing</p>';
		}
		if(isset($_POST['Production-Dispatching'])){
			$post .= '<p><label><b>Inquiry: </b></label>Production Dispatching</p>';
		}
		if(isset($_POST['Other-questions-about-human'])){
			$post .= '<p><label><b>Inquiry: </b></label>Other Questions About Human</p>';
		}
		 $post .= '<p><label><b>request</b></label>'. $_POST['request'] . '</p>';
		 $post .= '<p><label><b>job-content</b></label>'. $_POST['job-content'] . '</p>';
		 $post .= '<p><label><b>question</b></label>'. $_POST['question'] . '</p>';
		 $post .= '<p><label><b>company</b></label>'. $_POST['company'] . '</p>';
		 $post .= '<p><label><b>department</b></label>'. $_POST['department'] . '</p>';
		 $post .= '<p><label><b>job-title</b></label>'. $_POST['job-title'] . '</p>';
		 $post .= '<p><label><b>name</b></label>'. $_POST['name'] . '</p>';
		 $post .= '<p><label><b>phonetic</b></label>'. $_POST['phonetic'] . '</p>';
		 $post .= '<p><label><b>address</b></label>'. $_POST['address'] . '</p>';
		 $post .= '<p><label><b>contact</b></label>'. $_POST['contact'] . '</p>';
		$this->set('alias', $_POST['name']);
		$this->set('post', $post);
		if(!$this->Email->send()) {
			$this->Session->setFlash(__('Sending email failed. Please press the browser back button.'), 'default', array('class' => 'flashError'));
			$this->redirect(array('controller' => 'pages' , 'action' => 'contactus'));
		}else{
			$this->Session->setFlash(__('Thanks for you registration'), 'default', array('class' => 'flashError'));
			$this->redirect(array('controller' => '/'));
		}
	}
	
	function save(){
		if (!empty($this->data)) {
			$languages = $this->data['Staffoa'];
			$staffoas = $this->data['oaskill'];
			unset($this->data['Staffoa']);
			unset($this->data['oaskill']);
			$i = 0;
			foreach($languages as $key => $value):
				$this->data['StaffLanguage'][$i]['language_skill_id'] = $key;
				$this->data['StaffLanguage'][$i]['language_level_id'] = $value['language_skill'];
				$i++;
			endforeach;
			$i = 0;
			foreach($staffoas as $key => $value):
				$this->data['Staffoa'][$i]['oa_skill_id'] = $key;
				$this->data['Staffoa'][$i]['skill_level_id'] = $value['oa_skills'];
				$i++;
			endforeach;
			$this->Staff->create();
 			if ($this->Staff->saveAll($this->data)) {
 				$this->_email();
				$this->Session->setFlash(__('Thank You!! for Application', true));
				$this->redirect('/');
			} else {
				
			}
		}
	}

	function add($id = NULL) {
		if($id != NULL){
			$job_no = $id;
			$this->set(compact('job_no'));
		}
		$this->loadModel('LanguageSkill');
		$this->loadModel('OaSkill');
		$this->set('languages',$this->LanguageSkill->find('all'));
		$this->set('oaskills',$this->OaSkill->find('list', array('conditions' => array('parent <>' => NULL))));
		$this->set('skills', $this->OaSkill->SkillLevel->find('list'));
	}
	
	function _email() {
		$this->Email->from 		= '<test@ramhariregmi.com.np>';
		$this->Email->bcc 		= array('test1@ramhariregmi.com.np');
		$this->Email->sendAs 	= 'html';
		$this->Email->delivery 	= 'mail';
		$this->Email->template 	= 'default';
		$this->Email->to 		= $this->data['Staff']['pcemail'];
		$this->Email->subject	= 'Thanks for application -- Gud Luck.';
		$post = '<p>We will contact you if we think you as <b>suitable candidate.</b></p>';
        $post .= 'wait for our call';
		$this->set('alias', $this->data['Staff']['kanji']. ' ' . $this->data['Staff']['furigana']);
		$this->set('post', $post);
		if(!$this->Email->send()) {
			$this->Session->setFlash(__('Sending email failed. Please press the browser back button.'), 'default', array('class' => 'flashError'));
		}else{
			$this->Session->setFlash(__('Thanks for you registration'), 'default', array('class' => 'flashError'));
		}
	}
}
?>